package com.sunyur.tailor.channel.common.middle.util;

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.ext.DestinationDataProvider;
import com.sunyur.tailor.channel.common.middle.base.SapConn;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.util.Properties;

/**
 * @ClassName SAPConnUtils
 * @Description SAP调用工具类
 * @Author yu.chen
 * @Date 2021/3/5 14:15
 * @Version 1.0
 **/
@Slf4j
public class SAPConnUtils {
    private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
    /**
     * 创建SAP接口属性文件。
     * @param name  ABAP管道名称
     * @param suffix    属性文件后缀
     * @param properties    属性文件内容
     */
    private static void createDataFile(String name, String suffix, Properties properties){
        File cfg = new File(name+"."+suffix);
        log.info(cfg.getAbsolutePath());
        if(cfg.exists()){
            cfg.deleteOnExit();
        }
        try{
            FileOutputStream fos = new FileOutputStream(cfg, false);
            properties.store(fos, "for tests only !");
            fos.close();
        }catch (Exception e){
            log.error("Create Data file fault, error msg: {}", e);
            throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
        }
    }

    /**
     * 初始化SAP连接
     */
    private static void initProperties(SapConn sapConn) {
        Properties connectProperties = new Properties();
        // SAP服务器
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, sapConn.getJCO_ASHOST());
        // SAP系统编号
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  sapConn.getJCO_SYSNR());
        // SAP集团
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, sapConn.getJCO_CLIENT());
        // SAP用户名
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,   sapConn.getJCO_USER());
        // SAP密码
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, sapConn.getJCO_PASSWD());
        // SAP登录语言
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   sapConn.getJCO_LANG());
        // 最大连接数
        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, sapConn.getJCO_POOL_CAPACITY());
        // 最大连接线程
        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, sapConn.getJCO_PEAK_LIMIT());

        createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
    }

    /**
     * 获取SAP连接
     * @return  SAP连接对象
     */
    public static JCoDestination connect(SapConn sapConn){
        log.info("正在连接至SAP...");
        JCoDestination destination = null;
        SAPConnUtils.initProperties(sapConn);
        try {
            destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
            destination.ping();
            log.info("已成功建立sap的连接");
        } catch (JCoException e) {
            log.error("Connect SAP fault, error msg: {}", e);
        }
        return destination;
    }

    /**
     * @description 获取SAP函数
     * @param functionName
     * @return com.sap.conn.jco.JCoFunction
     * @author yu.chen
     * @date 2021/3/6 11:43
     * @Version v1.0
     */

    public static JCoFunction getFunction (JCoDestination jCoDestination, String functionName){
        JCoFunction jCoFunction = null;
        try {
            // 测试连接
            jCoFunction = jCoDestination.getRepository().getFunction(functionName);
        } catch (JCoException e) {
            log.error("Connect SAP getFunction, error msg: {}", e);
        }
        return jCoFunction;
    }
}
